//
//  FoodsStoreViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/25.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "FoodsStoreViewController.h"
#import "MovieCommentTableViewCell.h"
#import "FoodsStoreTableViewCell.h"
#import "CommitStarsView.h"
#import "AllMovieCommentViewController.h"
#import "PhotosViewController.h"
#import "PaperListViewController.h"
#import "ForSalePaperViewController.h"
#import "UMSocialScreenShoter.h"
#import "UMSocialShakeService.h"
#import "UMSocialControllerService.h"
#import "SharePopView.h"
#import "GroupShoppingDetailViewController.h"
static NSString *CellIdentify = @"FoodsStoreTableViewCell";
static NSString *cellIdentify = @"MovieCommentTableViewCell";
@interface FoodsStoreViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>{
    NSInteger _totle;
}
@property (nonatomic,assign)   NSInteger    pageNO;
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIView *hotelImageView;
@property (weak, nonatomic) IBOutlet UIImageView *hotelImage;
@property(nonatomic,assign)CGFloat imageScale;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
@property (weak, nonatomic) IBOutlet UIButton *collectButton;
@property (weak, nonatomic) IBOutlet UIButton *whitCollection;
@property (strong, nonatomic) IBOutlet UITableView *foodsTableView;
@property (strong, nonatomic) IBOutlet UITableView *commentTableView;

@property (strong, nonatomic) IBOutlet UIImageView *storeImage;//商家图片
@property (strong, nonatomic) IBOutlet UILabel *storeName;//商家名字
@property (strong, nonatomic) IBOutlet UILabel *storeScore;//商家评分
@property (strong, nonatomic) IBOutlet UILabel *storeIntroduce;//商家简介

@property (strong, nonatomic) IBOutlet UILabel *workTime;//营业时间
@property (strong, nonatomic) IBOutlet UILabel *storeAddress;//商家地址
@property (strong, nonatomic) IBOutlet UILabel *storePhone;//商家电话
@property (weak, nonatomic) IBOutlet UILabel *storeTag;//商家标签

@property (strong, nonatomic) IBOutlet UIView *buttonView;
@property (strong, nonatomic) IBOutlet UIButton *buttonOne;
@property (strong, nonatomic) IBOutlet UIButton *buttonTwo;
@property (strong, nonatomic) IBOutlet UIButton *buttonThree;
@property (strong, nonatomic) IBOutlet UIView *sliderLabel;
@property (strong, nonatomic) IBOutlet UILabel *bottomLabel;




@property (strong, nonatomic) IBOutlet UILabel *commentScore;//评论分数
@property (weak, nonatomic) IBOutlet UILabel *commentTotalNum;//评论总数
@property (weak, nonatomic) IBOutlet UIView *commentView;
@property (weak, nonatomic) IBOutlet UIButton *moreCommentButton;
@property (weak, nonatomic) IBOutlet UILabel *detailDesTitle;
@property (weak, nonatomic) IBOutlet UIImageView *detailDesImage;
@property (weak, nonatomic) IBOutlet UILabel *detailDes;


@property (strong, nonatomic) IBOutlet UIImageView *imageNumberBackView;
@property (strong, nonatomic) IBOutlet UILabel *imageNumber;
@property(nonatomic,strong)NSArray * imageArray;

@property(nonatomic,strong)NSMutableArray * commitArray;
@property(nonatomic,strong)NSMutableArray * heightArray;

@property(nonatomic,strong)NSMutableArray * groupShops;
@property(nonatomic,strong)NSArray * pricePaperArray;

@end

@implementation FoodsStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageArray = [NSArray array];
    _pricePaperArray = [NSArray array];
    _heightArray = [NSMutableArray array];
    _commitArray = [NSMutableArray array];
    _groupShops = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self initViewLayout];
    [self getStoreInfo];
    [self getComments];
    [self ifCollected];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.hotelImageView];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.commentView];
    [self adaptiveViewLayout:self.buttonView];
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 1231), NO);
    
    
    _imageNumberBackView.layer.cornerRadius = 2*VerticalRatio();
    
    
    //注册
    [self.commentTableView registerNib:[UINib nibWithNibName:cellIdentify bundle:nil] forCellReuseIdentifier:cellIdentify];
    _commentTableView.dataSource = self;
    _commentTableView.delegate = self;
    
    [self.foodsTableView registerNib:[UINib nibWithNibName:CellIdentify bundle:nil] forCellReuseIdentifier:CellIdentify];
    _foodsTableView.dataSource = self;
    _foodsTableView.delegate = self;
    
    
    _foodsTableView.scrollEnabled = NO;
    _scroView.delegate = self;
    
    _imageScale = CGRectGetWidth(_hotelImage.frame)/CGRectGetHeight(_hotelImage.frame);
    
}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}


#pragma mark == 店铺详情
- (void)getStoreInfo{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_foodId forKey:@"storeId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppBusinessDetailsInfo withHttpType:@"POST" withTag:SX_SX_queryAppBusinessDetailsInfo];
    
}


#pragma mark ====查询商家是否收藏
- (void)ifCollected{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_foodId forKey:@"storeId"];
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryStoreIsCollect withHttpType:@"POST" withTag:SX_SX_queryStoreIsCollect];
    
}

#pragma mark == 店铺团购项目 代金劵
- (void)getStoreGroupIndent{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic  setObject:[NSNumber numberWithInteger:self.pageNO]     forKey:@"currentPage"];
    [dic  setObject:[NSNumber numberWithInteger:PageSize]       forKey:@"pageSize"];
    [dic setObject:_foodId forKey:@"storeId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryGrouponProject withHttpType:@"POST" withTag:SX_SX_queryGrouponProject];
    
}

#pragma mark ==== 收藏
- (IBAction)collectHandel:(id)sender {
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:@"addGoodsCollectInfo" forKey:@"method"];
    [dic setObject:@"2" forKey:@"collectType"];
    [dic setObject:_foodId forKey:@"goodsId"];
    [dic setObject:@"2" forKey:@"storeType"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_AddGoodsCollectInfo withHttpType:@"POST" withTag:SX_AddGoodsCollectInfoTag];
}

#pragma mark == 分享
- (IBAction)shareHandel:(id)sender {
    SharePopView * popView = [[SharePopView alloc]initWithTitles:@[@"微信好友",@"朋友圈",@"新浪微博",@"QQ好友"] complect:^(NSInteger index) {
        switch (index) {
            case 0:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
                
            }
                break;
            case 1:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 2:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToSina] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 3:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            default:
                break;
        }

    }];
    [popView show];
}


#pragma mark == 获取商家评论
- (void)getComments{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_foodId forKey:@"storeId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppMallGrouponCommentByStoreId withHttpType:@"POST" withTag:SX_SX_queryAppMallGrouponCommentByStoreId];
    
}

#pragma mark == 查看全部评论
- (IBAction)showAllComment:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    AllMovieCommentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AllMovieComment"];
    page.commentId = _foodId;
    page.type = 3;
    [self.navigationController   pushViewController:page animated:YES];
}

#pragma mark == 切换评论  优惠
- (IBAction)button:(UIButton *)sender {
    if (sender == _buttonOne) {
        [UIView animateWithDuration:0.2 animations:^{
            _sliderLabel.frame = flexibleFrame(CGRectMake(32, 42, 40, 2), YES);
        }];
        _buttonOne.selected = YES;
        _buttonTwo.selected = NO;
        _buttonThree.selected = NO;
        [_scroView setContentOffset:CGPointMake(0*VerticalRatio(),77*VerticalRatio()) animated:YES];
    } else if (sender == _buttonTwo) {
        
        if (_commitArray.count==0) {
            [CWHUDTips showTips:@"暂无评论"];
            return;
        }
        
        [UIView animateWithDuration:0.2 animations:^{
            _sliderLabel.frame = flexibleFrame(CGRectMake(140, 42, 40, 2), YES);
        }];
        _buttonOne.selected = NO;
        _buttonTwo.selected = YES;
        _buttonThree.selected = NO;
        [_scroView setContentOffset:CGPointMake(0,CGRectGetMinY(_commentView.frame)) animated:YES];
    } else if (sender == _buttonThree) {
        
        if (_groupShops.count==0) {
                [CWHUDTips showTips:@"暂无优惠"];
                return;
        }
        
        [UIView animateWithDuration:0.2 animations:^{
            _sliderLabel.frame = flexibleFrame(CGRectMake(246, 42, 40, 2), YES);
        }];
        _buttonOne.selected = NO;
        _buttonTwo.selected = NO;
        _buttonThree.selected = YES;
        [_scroView setContentOffset:CGPointMake(0,CGRectGetMinY(_foodsTableView.frame)) animated:YES];
    }
}

#pragma mark == 商家相册
- (IBAction)photos:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    PhotosViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Photos"];
    page.imagesArray = _imageArray;
    page.photosType = @"1";
    [self.navigationController   pushViewController:page animated:YES];
}

#pragma mark == 拨打电话
- (IBAction)callHandel:(id)sender {
    [Common callPhone:_storePhone.text view:self.view];
}
#pragma mark == 跳转代金卷
- (IBAction)getForSalePaper:(id)sender {
    if (_pricePaperArray.count>0) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
        PaperListViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PaperList"];
        page.paperArray = _pricePaperArray ;
        [self.navigationController   pushViewController:page animated:YES];
    }else{
//    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
//    ForSalePaperViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ForSalePaper"];
//    [self.navigationController   pushViewController:page animated:YES];
    }
}

#pragma mark  ==== scroViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset = scrollView.contentOffset.y;
    if (offset <= 0) {
        _navView.alpha = 0.011;
        CGFloat newHeight = 142-offset;
        CGFloat newWidth = newHeight*_imageScale;
        _hotelImage.frame = flexibleFrame(CGRectMake(-(newWidth-320)/2, -(newHeight-142)/2, newWidth, newHeight), NO);
        
    }else if (offset<64*VerticalRatio()&&offset>0) {
        _navView.alpha = offset/(64*VerticalRatio());
    }else{
        _navView.alpha = 1;
    }
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    if (scrollView == _scroView ) {
//        if (scrollView.contentOffset.y>_offSet) {
//
//            [_scroView setContentOffset:CGPointMake(0, 64*VerticalRatio()) animated:YES];
//            _scroView.scrollEnabled = NO;
//        }else{
//            if (scrollView == _scroView) {
//                [_scroView setContentOffset:CGPointMake(0, 0) animated:YES];
//                _scroView.scrollEnabled = YES;
//            }else{
//                if (decelerate == YES) {
//                    [_scroView setContentOffset:CGPointMake(0, 0) animated:YES];
//                    _scroView.scrollEnabled = YES;
//                }
//            }
//
//        }
//    }
//}
//

#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _commentTableView) {
        CGFloat height= [_heightArray[indexPath.row] floatValue]*VerticalRatio();
        return height ;
    }else{
        return 62*VerticalRatio();
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView != _commentTableView) {
        return _groupShops.count;
    }else{
        if (_commitArray.count<=2) {
            return _commitArray.count;
        }else{
            return 2;
        }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView != _commentTableView) {
        FoodsStoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify ];
        if (cell==nil) {
            cell = [[FoodsStoreTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentify];
            
        }
            for (UIView *view in cell.subviews) {
                if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
                    [view removeFromSuperview];
                }
            }
        NSDictionary * dic = _groupShops[indexPath.row];
        cell.shopName.frame = flexibleFrame(CGRectMake(115, 13, 197, 0), NO);
        cell.shopName.text = [NSString stringWithFormat:@"%@",dic[@"mgprName"]];
        [cell.shopName sizeToFit];
        
        NSString * image = [NSString stringWithFormat:@"%@",dic[@"mgimPhoneImg"]];
        if (image.length>0) {
            [cell.shopImage sd_setImageWithURL:[NSURL URLWithString:image]];
        }
        NSString *mgpiGourponAmount = [Common stringFromData:dic[@"mgpiGourponAmount"]];

        NSString *mgpiOrginalAmount = [Common stringFromData:dic[@"mgpiOrginalAmount"]];
        NSMutableAttributedString * str=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@ 门市价:￥%@",mgpiGourponAmount,mgpiOrginalAmount]];
        [str addAttribute:NSForegroundColorAttributeName value:kGetColor(240, 59, 59) range:NSMakeRange(0, mgpiGourponAmount.length+1)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16*VerticalTexeRatio()] range:NSMakeRange(0, mgpiGourponAmount.length+1)];
        cell.shopPrice.attributedText = str;
    
        cell.soldNum.text = [NSString stringWithFormat:@"已售%ld",[[Common stringFromData:dic[@"mgprSelledcount"]]integerValue]];
        
        NSString * mgprType = [Common stringFromData:_groupShops[indexPath.row][@"mgprTypeId"]];
        
        cell.typeLabel.backgroundColor = kGetColor(246, 160, 57);
        cell.typeLabel.text = @"券";
        if ([mgprType isEqualToString:@"20160413"]) {//代金券
            cell.typeLabel.backgroundColor = kGetColor(246, 160, 57);
            cell.typeLabel.text = @"券";
        }else{//团购
            cell.typeLabel.backgroundColor = kGetColor(116, 193, 96);
            cell.typeLabel.text = @"团";
        }


        return cell;
    }else{
    MovieCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify ];
    if (cell == nil) {
        cell = [[MovieCommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
        for (UIView *view in cell.subviews) {
            if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
                [view removeFromSuperview];
            }
        }
        NSDictionary * dic = _commitArray[indexPath.row];
        NSString * image = dic[@"memberImg"];
        if (image) {
            [cell.headImage sd_setImageWithURL:[NSURL URLWithString:image]];
        }else{
            cell.headImage.image = [UIImage imageNamed:@"comment1"];
        }
        double star = [[NSString stringWithFormat:@"%.1f",[dic[@"mgcoCommentStars"]floatValue ]/2]doubleValue];
        
        CommitStarsView * stars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(13, 50, 80, 15), NO) Stars:star];
        [cell addSubview:stars];
        
        cell.commentInfo.frame = flexibleFrame(CGRectMake(10, 72, 300, 15), YES);
        cell.commentInfo.numberOfLines = 0;
        cell.commentInfo.text = dic[@"mgcoContent"];
        [cell.commentInfo sizeToFit];
        
        cell.name.text = dic[@"mgcoAuthor"];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[dic[@"commentDate"] integerValue]/1000];
        cell.date.text = [formatter stringFromDate:confromTimesp];
//        
    
    return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * mgprType = [Common stringFromData:_groupShops[indexPath.row][@"mgprTypeId"]];
    if ([mgprType isEqualToString:@"20160413"]) {//代金券
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
        ForSalePaperViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ForSalePape"];
        NSDictionary * dic = _groupShops[indexPath.row];
        page.groupShoppingId = dic[@"mgprId"];
        [self.navigationController   pushViewController:page animated:YES];
    }else{//团购
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
        GroupShoppingDetailViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"GroupShoppingDetail"];
        NSDictionary * dic = _groupShops[indexPath.row];
        page.groupShoppingId = dic[@"mgprId"];
        page.goodsStoreId = _foodId;
        [self.navigationController   pushViewController:page animated:YES];
    }
    
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    
    if (tag==SX_SX_queryAppBusinessDetailsInfo) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _imageArray = database[@"result"][@"list"];
            if (![_imageArray isEqual:[NSNull null]]) {
                _imageNumber.text = [NSString stringWithFormat:@"共%ld张",_imageArray.count];
                [_imageNumberBackView sd_setImageWithURL:[NSURL URLWithString:[Common stringFromData:_imageArray[0]]]];
            }
            
            NSDictionary * dic = database[@"result"];
            NSString * image  = [Common stringFromData:dic[@"storeImg"]];
            if (image.length>0) {
                [_hotelImage sd_setImageWithURL:[NSURL URLWithString:image]];
                [_detailDesImage sd_setImageWithURL:[NSURL URLWithString:image]];

            }
            
            NSString * logoimage  = [Common stringFromData:dic[@"storeLogo"]];
            if (logoimage.length>0) {
                [_storeImage sd_setImageWithURL:[NSURL URLWithString:logoimage]];
            }
            
            _storeName.text = [NSString stringWithFormat:@"%@",dic[@"storeName"]];
            _detailDesTitle.text = [NSString stringWithFormat:@"%@",dic[@"storeName"]];
            NSString *storeType = dic[@"storeType"];
            if ([dic[@"storeType"] isEqual:[NSNull null]]) {
                storeType = @"中餐";
            }
            NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  人均:¥%@",storeType,dic[@"msgoPerConsumption"]]];
            [str addAttribute:NSForegroundColorAttributeName value:kGetColor(243, 146, 0) range:NSMakeRange(5+storeType.length, str.length-5-storeType.length)];
            
            _storeIntroduce.attributedText = str;

            double start = [[NSString stringWithFormat:@"%.1f",[dic[@"storeCommAvg"]floatValue]/2] doubleValue];
            CommitStarsView * CommentStars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(68, 116, 65, 15), NO) SmallStars:start];
            [self.hotelImageView addSubview:CommentStars];
            _storeScore.text = [NSString stringWithFormat:@"%.1f分",start];
            _commentScore.text = [NSString stringWithFormat:@"%.1f分",start];
            _workTime.text = [NSString stringWithFormat:@"%@:%@-%@:%@营业",dic[@"msgoBeginHour"],dic[@"msgoBeignMinute"],dic[@"msgoEndHour"],dic[@"msgoEndMinute"]];
            _storeAddress.text = [NSString stringWithFormat:@"%@",dic[@"storeAddress"]];
            _storePhone.text = [NSString stringWithFormat:@"%@",dic[@"storePhone"]];
            _storeTag.text = [Common stringFromData:dic[@"storeSimpleDescription"]];
            _detailDes.text = [Common stringFromData:dic[@"storeDescription"]];
            
            
            
        }
    }else if (tag == SX_SX_queryAppMallGrouponCommentByStoreId){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _commitArray = database[@"result"][@"comment"];
            _commentTotalNum.text = [NSString stringWithFormat:@"评价（%ld）",_commitArray.count];
            _moreCommentButton.layer.borderColor = kGetColor(244, 244, 244).CGColor;
            _moreCommentButton.layer.borderWidth = 1;
            
            NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"查看全部%ld评价",_commitArray.count]];
            // 创建图片图片附件
            NSTextAttachment *attach = [[NSTextAttachment alloc] init];
            attach.image = [UIImage imageNamed:@"Business details3"];
            attach.bounds = flexibleFrame(CGRectMake(1, -1, 6, 10), NO);
            NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
            [str appendAttributedString:attachString];
            [_moreCommentButton setAttributedTitle:str forState:UIControlStateNormal];
            
            double start = [[NSString stringWithFormat:@"%.1f",[database[@"result"][@"commentStar"]floatValue]/2] doubleValue];
            CommitStarsView * CommentStars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(197, 17, 65, 15), NO) SmallStars:start];
            [self.commentView addSubview:CommentStars];

            _commentScore.text = [NSString stringWithFormat:@"%.1f分",start];
            
            for (int i = 0; i<_commitArray.count; i++) {
                NSString * commentImage = _commitArray[i][@"mgciImag"];
                NSString * comment = _commitArray[i][@"mgcoContent"];
                NSInteger height = 0;
                if (commentImage.length==0 ) {
                    height = 90;
                }else{
                    height = 130;
                }
                
                
                int lines = 0;
                CGSize cSize = [comment sizeWithFont:[UIFont systemFontOfSize:14*VerticalTexeRatio()]];
                lines = cSize.width/300*VerticalRatio() + 1 ;
                
                height  += lines * 17;
                
                [_heightArray addObject:@(height)];
            }
            
            if (_heightArray.count>0) {
                NSInteger rows = _heightArray.count;
                if (_heightArray.count>2) {
                    rows = 2;
                }
                CGFloat Height = 0;
                for (int i = 0; i<rows; i++) {
                    CGFloat height = [_heightArray[i]floatValue];
                    Height = Height+height;
                }
                _commentTableView.frame = flexibleFrame(CGRectMake(0, 40, 320, Height), NO);
                _moreCommentButton.frame = flexibleFrame(CGRectMake(-1, CGRectGetMaxY(_commentTableView.frame)/VerticalRatio(), 322, CGRectGetHeight(_moreCommentButton.frame)/VerticalRatio()), NO);
                _commentView.frame = flexibleFrame(CGRectMake(0, CGRectGetMinY(_commentView.frame)/VerticalRatio(), 320, Height+45+36), NO);
                _foodsTableView.frame = flexibleFrame(CGRectMake(0, CGRectGetMaxY(_commentView.frame)/VerticalRatio(), 320, CGRectGetHeight(_foodsTableView.frame)/VerticalRatio()), NO);
                _scroView.contentSize = CGSizeMake(0, CGRectGetMaxY(_foodsTableView.frame)+1);
                _commentTableView.delegate = self;
                _commentTableView.dataSource = self;
                [_commentTableView reloadData];
            }else{
                _commentView.frame = flexibleFrame(CGRectMake(0, CGRectGetMinY(_commentView.frame)/VerticalRatio(), 320, 5), NO);
                [_commentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 5), NO)];
                line.backgroundColor = kGetColor(244, 244, 244);
                [_commentView addSubview:line];
                
                _foodsTableView.frame = flexibleFrame(CGRectMake(0, CGRectGetMinY(_commentView.frame)/VerticalRatio()+5, 320, CGRectGetHeight(_foodsTableView.frame)/VerticalRatio()), NO);
                _scroView.contentSize = CGSizeMake(0, CGRectGetMaxY(_foodsTableView.frame)+1);
            }
           
            [self getStoreGroupIndent];
 
        }
        
    }else if (tag == SX_SX_queryGrouponProject){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _groupShops = database[@"result"][@"pageRecord"];
            _foodsTableView.frame = flexibleFrame(CGRectMake(0, CGRectGetMinY(_foodsTableView.frame)/VerticalRatio(), 320, 62*_groupShops.count), NO);
            _scroView.contentSize = CGSizeMake(0, CGRectGetMaxY(_foodsTableView.frame));
            _foodsTableView.dataSource = self;
            _foodsTableView.delegate = self;
            
            [_foodsTableView reloadData];
        }
    
    
    }else if (tag == SX_AddGoodsCollectInfoTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            _collectButton.selected = !_collectButton.selected;
            _whitCollection.selected = !_whitCollection.selected;
        }
    }else if (tag == SX_SX_queryStoreIsCollect){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSString * status = database[@"result"];
            if ([status isEqualToString:@"1"]) {
                _collectButton.selected = YES;
                _whitCollection.selected = YES;
            }else{
                _collectButton.selected = NO;
                _whitCollection.selected = NO;

            }
        }
        
    }

}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
        
        //
        [self dismissWaitDialog];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
